home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #11 / Amiga Plus CD - 2002 - No. 11.iso / Tools / Development / TinyGL / ami / content / ad709 / tinygl / src / list.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-15  |  3.2 KB  |  197 lines

  1. /*$T list.c GC 1.137 08/09/02 17:47:18 */
  2.  
  3. /*$6
  4.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  5.  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  6.  */
  7.  
  8. #include "zgl.h"
  9. #include "list.h"
  10.  
  11. static char *op_table_str[] =
  12. {
  13. #define ADD_OP(a, b, c) "gl"#a " "#c,
  14.  
  15. #include "opinfo.h"
  16. };
  17.  
  18. static void (*op_table_func[]) (GLContext *, GLParam *) =
  19. {
  20. #define ADD_OP(a, b, c) glop ## a,
  21.  
  22. #include "opinfo.h"
  23. };
  24.  
  25. static int    op_table_size[] =
  26. {
  27. #define ADD_OP(a, b, c) b + 1,
  28.  
  29. #include "opinfo.h"
  30. };
  31.  
  32. /* */
  33.  
  34. GLContext *gl_get_context(void) {
  35.     return gl_ctx;
  36. }
  37.  
  38. /* */
  39. GLList *find_list(GLContext *c, unsigned int list) {
  40.     return c->shared_state.lists[list];
  41. }
  42.  
  43. /* */
  44. void delete_list(GLContext *c, int list) {
  45.     GLParamBuffer    *pb, *pb1;
  46.     GLList            *l;
  47.  
  48.     l = find_list(c, list);
  49.     assert(l != NULL);
  50.  
  51.     /* free param buffer */
  52.     pb = l->first_op_buffer;
  53.     while(pb != NULL) {
  54.         pb1 = pb->next;
  55.         free(pb);
  56.         pb = pb1;
  57.     }
  58.  
  59.     free(l);
  60.     c->shared_state.lists[list] = NULL;
  61. }
  62.  
  63. /* */
  64. GLList *alloc_list(GLContext *c, int list) {
  65.     GLList            *l;
  66.     GLParamBuffer    *ob;
  67.  
  68.     l = calloc(1, sizeof(GLList));
  69.     ob = calloc(1, sizeof(GLParamBuffer));
  70.  
  71.     ob->next = NULL;
  72.     l->first_op_buffer = ob;
  73.  
  74.     ob->ops[0].op = OP_EndList;
  75.  
  76.     c->shared_state.lists[list] = l;
  77.     return l;
  78. }
  79.  
  80. /* */
  81. void gl_print_op(FILE *f, GLParam *p) {
  82.     int        op;
  83.     char    *s;
  84.  
  85.     op = p[0].op;
  86.     p++;
  87.     s = op_table_str[op];
  88.     while(*s != 0) {
  89.         if(*s == '%') {
  90.             s++;
  91.             switch(*s++) {
  92.             case 'f':    fprintf(f, "%g", p[0].f); break;
  93.             default:    fprintf(f, "%d", p[0].i); break;
  94.             }
  95.  
  96.             p++;
  97.         }
  98.         else {
  99.             fputc(*s, f);
  100.             s++;
  101.         }
  102.     }
  103.  
  104.     fprintf(f, "\n");
  105. }
  106.  
  107. /* */
  108. void gl_compile_op(GLContext *c, GLParam *p) {
  109.     int                op, op_size;
  110.     GLParamBuffer    *ob, *ob1;
  111.     int                index, i;
  112.  
  113.     op = p[0].op;
  114.     op_size = op_table_size[op];
  115.     index = c->current_op_buffer_index;
  116.     ob = c->current_op_buffer;
  117.  
  118.     /* we should be able to add a NextBuffer opcode */
  119.     if((index + op_size) > (OP_BUFFER_MAX_SIZE - 2)) {
  120.         ob1 = calloc(1, sizeof(GLParamBuffer));
  121.         ob1->next = NULL;
  122.  
  123.         ob->next = ob1;
  124.         ob->ops[index].op = OP_NextBuffer;
  125.         ob->ops[index + 1].p = (void *) ob1;
  126.  
  127.         c->current_op_buffer = ob1;
  128.         ob = ob1;
  129.         index = 0;
  130.     }
  131.  
  132.     for(i = 0; i < op_size; i++) {
  133.         ob->ops[index] = p[i];
  134.         index++;
  135.     }
  136.  
  137.     c->current_op_buffer_index = index;
  138. }
  139.  
  140. /* */
  141. void gl_add_op(GLParam *p) {
  142.     GLContext    *c = gl_get_context();
  143.     int            op;
  144.  
  145.     op = p[0].op;
  146.     if(c->exec_flag) {
  147.         op_table_func[op](c, p);
  148.     }
  149.  
  150.     if(c->compile_flag) {
  151.         gl_compile_op(c, p);
  152.     }
  153.  
  154.     if(c->print_flag) {
  155.         gl_print_op(stderr, p);
  156.     }
  157. }
  158.  
  159. /* this opcode is never called directly */
  160. void glopEndList(GLContext *c, GLParam *p) {
  161.     assert(0);
  162. }
  163.  
  164. /* this opcode is never called directly */
  165. void glopNextBuffer(GLContext *c, GLParam *p) {
  166.     assert(0);
  167. }
  168.  
  169. /* */
  170. void glopCallList(GLContext *c, GLParam *p) {
  171.     GLList    *l;
  172.     int        list, op;
  173.  
  174.     list = p[1].ui;
  175.     l = find_list(c, list);
  176.     if(l == NULL) {
  177.         gl_fatal_error("list %d not defined", list);
  178.     }
  179.  
  180.     p = l->first_op_buffer->ops;
  181.  
  182.     while(1) {
  183.         op = p[0].op;
  184.         if(op == OP_EndList) {
  185.             break;
  186.         }
  187.  
  188.         if(op == OP_NextBuffer) {
  189.             p = (GLParam *) p[1].p;
  190.         }
  191.         else {
  192.             op_table_func[op](c, p);
  193.             p += op_table_size[op];
  194.         }
  195.     }
  196. }
  197.